package mani.multithreading;

class Counter implements Runnable {
	 volatile int count=1;
	  static Class<Counter> cl = Counter.class;
   

	  public synchronized void increment() {
	        String tname = Thread.currentThread().getName();
	        System.out.printf("%s:%d\n", tname, count++);
	        
	  }

	  @Override
	  public void run() {
	       
	        while (true) {

	        	 increment();
	              synchronized (Counter.class) {
	            	 
	                    try {
	                          cl.notify();
	                          cl.wait();
	                          if(count>=10){
	                        	  System.exit(0);
	                          }
	                    } catch (Exception e) {
	                          e.printStackTrace();
	                    }
	              }

	        }
	  }
	}

	public class WaitNotify {
	public static void main(String[] args) {
	    Counter c = new Counter();
	    Thread t1 = new Thread(c, "thread1");
	    Thread t2 = new Thread(c, "thread2");
	    Thread t3 = new Thread(c, "thread3");
	    
	    t1.start();
	    t2.start();
	    t3.start();
	}
	}